package com.matt.sooncleaner.wifiscan

import android.os.Bundle
import com.matt.sooncleaner.arch.ArchFragment
import com.matt.sooncleaner.databinding.FragmentWifiScanBinding
import com.trustlook.wifisdk.wifiscan.WifiItem
import com.trustlook.wifisdk.wifiscan.WifiScanClient
import com.trustlook.wifisdk.wifiscan.WifiScanListener
import com.trustlook.wifisdk.wifiscan.WifiScanResult

class WifiScanFragment: ArchFragment<FragmentWifiScanBinding>() {
    interface OnWifiScanListener {
        fun onScanFinish(wifiScanResult: WifiScanResult)
    }

    private var mOnWifiScanListener: OnWifiScanListener? = null
    private lateinit var mWifiScanClient: WifiScanClient

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mWifiScanClient = WifiScanClient.Builder(requireContext().applicationContext)
            .build()
    }

    override fun onInitView(binding: FragmentWifiScanBinding) {
        super.onInitView(binding)
        binding.wifiScan.playAnimation()
        binding.progressView.setMax(100f)
        binding.progressView.setShowZero(false)
        binding.progressView.setProgressAnimationDuration(5000)
        mWifiScanClient.startWifiScan(object : WifiScanListener() {
            override fun onScanStarted() {
            }

            override fun onScanReady(curSid: String) {
            }

            override fun onScanProgress(curr: Int, total: Int, item: WifiItem?) {
                println("current: ")
                if (curr == 1) {
                    binding.progressView.setProgress(1f, false)
                } else {
                    val progress = (curr-1f)/total*100f
                    binding.progressView.setProgress(progress, true)
                    if (curr == total) {
                        binding.progressView.setProgressAnimationDuration(30000)
                        binding.progressView.setProgressEndListener {
                            binding.progressView.setProgress(100f)
                        }
                    }
                }
            }

            override fun onScanError(errCode: Int) {
                println("scan error: $errCode")
            }

            override fun onScanCanceled() {
            }

            override fun onScanInterrupt() {
            }

            override fun onScanFinished(wifiScanResult: WifiScanResult?) {
                if (!isVisible) {
                    return
                }
                saveWifiScanTime()
                binding.progressView.setProgressAnimationDuration(1000)
                binding.progressView.setProgress(binding.progressView.getMax())
                binding.progressView.setProgressEndListener {
                    mWifiScanClient.cancelScan()
                    wifiScanResult?.let {
                        mOnWifiScanListener?.onScanFinish(it)
                    }
                }
            }

        })
    }

    fun setOnWifiScanListener(listener: OnWifiScanListener) {
        mOnWifiScanListener = listener
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mWifiScanClient.cancelScan()
    }
}